#include <afxcoll.h>
#include <tchar.h>
#include "Listing.h"
#include "ListingDef.h"

LISTING_t Listing;

BOOL GetListRecursive(TCHAR* pszPath, unsigned long* pNumberFolders, unsigned long* pNumberFiles)
{
	memset(&Listing, 0, sizeof(LISTING_t));
	if (FALSE == ListRecursive(pszPath))
	{
		return FALSE;
	}
	*pNumberFolders = Listing.nNumberFolders;
	*pNumberFiles   = Listing.nNumberFiles;

	return TRUE;
}

BOOL ListRecursive(TCHAR* pszPath)
{
	TCHAR szTempPath[MAX_PATH] = {0};
	HANDLE hFind = NULL;
	WIN32_FIND_DATA fdFile;

	wsprintf(szTempPath, _T("%s\\*.*"), pszPath);
	if (INVALID_HANDLE_VALUE == (hFind = FindFirstFile(szTempPath, &fdFile)))
	{
		return FALSE;
	}
	do
	{
		if ((_tcscmp(fdFile.cFileName, _T(".")) != 0) && (_tcscmp(fdFile.cFileName, _T("..")) != 0))
		{
			wsprintf(szTempPath, _T("%s\\%s"), pszPath, fdFile.cFileName);
			if (FILE_ATTRIBUTE_DIRECTORY == (FILE_ATTRIBUTE_DIRECTORY & fdFile.dwFileAttributes))
			{
				Listing.nNumberFolders++;
				ListRecursive(szTempPath);
			}
			else
			{
				//wcscpy(pszPath, szTempPath);
				Listing.nNumberFiles++;
			}
		}
	} while (FindNextFile(hFind, &fdFile));
	FindClose(hFind);

	return TRUE;
}

BOOL GetListRecursiveX(TCHAR* pszPath, CStringArray* pLstFiles, unsigned long* pNumberFolders, unsigned long* pNumberFiles)
{
	memset(&Listing, 0, sizeof(LISTING_t));
	if (FALSE == ListRecursiveX(pszPath, pLstFiles))
	{
		return FALSE;
	}
	*pNumberFolders = Listing.nNumberFolders;
	*pNumberFiles = Listing.nNumberFiles;

	return TRUE;
}

BOOL ListRecursiveX(TCHAR* pszPath, CStringArray* pLstFiles)
{
	TCHAR szTempPath[MAX_PATH] = { 0 };
	HANDLE hFind = NULL;
	WIN32_FIND_DATA fdFile;

	wsprintf(szTempPath, _T("%s\\*.*"), pszPath);
	if (INVALID_HANDLE_VALUE == (hFind = FindFirstFile(szTempPath, &fdFile)))
	{
		return FALSE;
	}
	do
	{
		if ((_tcscmp(fdFile.cFileName, _T(".")) != 0) && (_tcscmp(fdFile.cFileName, _T("..")) != 0))
		{
			wsprintf(szTempPath, _T("%s\\%s"), pszPath, fdFile.cFileName);
			if (FILE_ATTRIBUTE_DIRECTORY == (FILE_ATTRIBUTE_DIRECTORY & fdFile.dwFileAttributes))
			{
				Listing.nNumberFolders++;
				ListRecursiveX(szTempPath, pLstFiles);
			}
			else
			{
				pLstFiles->Add(szTempPath);
				Listing.nNumberFiles++;
			}
		}
	} while (FindNextFile(hFind, &fdFile));
	FindClose(hFind);

	return TRUE;
}